package br.sed.theenemy.presentation;


/**
 * 
 * @author JulioCB
 * @email jcbjulio@gmail.com
 * 
 */
public class LoginFilter {
    
//	private static final String USER_NAME = "Admin.userName";
//    private static final String ACCESSIBLE_FUNCTIONS = "Admin.accessibleFunctions";
//    private static final String FORWARD_SLASH = "/";
//    
//
//    /**
//     * Destroy implementation.
//     */
//    public void destroy() {
//        
//    }
//    
//    /**
//     * Method that executes the filter control access to administrative tool.
//     * 
//     * Verification flow:
//     * 
//     * - If the user is not logged in
//     *         * Redirects the user to the login page
//     * 
//     * - If the user is logged in
//     *         * If the page is accessed "home" or "error page", the user just have to be logged
//     *         * If this is a page-specific functionality, checks whether the user has access to this 
//     *           functionality, according to your profile.
//     *             - If the user does not have access to the functionality (eg user typed the URL directly to a 
//     *               feature that your profile does not allow access), the page redirects to internal error 
//     *  
//     * @param sRequest ServletRequest
//     * @param sResponse ServletResponse
//     * @param chain FilterChain
//     * @throws IOException IOException
//     * @throws ServletException ServletException
//     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
//     */
//    public void doFilter(final ServletRequest sRequest, final ServletResponse sResponse,
//            final FilterChain chain) throws IOException, ServletException {
//        final HttpServletRequest request = (HttpServletRequest) sRequest;
//        final HttpServletResponse response = (HttpServletResponse) sResponse;
//
//        final AdminSession session = new AdminSession(request, false);
//        final AdminUser adminUser = session.getLoggedAdminUser();
//
//        if (adminUser == null) {
//            // User not logged in
//            response.sendRedirect(adminPresentationUtil.getLoginPath());
//
//        } else {
//            //User logged in
//            final String uri = request.getRequestURI();
//            final String contextPath = request.getContextPath();
//            
//            final int beginIndex = uri.indexOf(FORWARD_SLASH) + contextPath.length() + 1;
//            final String actionPath = uri.substring(beginIndex);
//            
//            //uri = uri.split(contextPath + "/")[1];
//            //If access is to the home of the tool or to the error page, simply be logged
//            if (adminPresentationUtil.getBasePath().endsWith(actionPath) ||
//                    adminPresentationUtil.getErrorPath().endsWith(actionPath)) {
//                
//                if (adminPresentationUtil.getBasePath().endsWith(actionPath) ){
//                    setMenuFieldsAndUserName(request, adminUser, null);
//                }
//                chain.doFilter(sRequest, sResponse);            
//            } else {
//                // Otherwise, it refers to a specific feature - check permission
//                final boolean hasAccess = adminFacade.hasAccessToFunction(
//                        adminUser.getId(), actionPath);
//                
//                if (hasAccess) {
//                    //User logged in and allowed to access the page
//                    setMenuFieldsAndUserName(request, adminUser, actionPath);
//                    chain.doFilter(sRequest, sResponse);
//                } else {
//                    // User logged in, but without access to functionality
//                    // Redirect to internal error page
//                    response.sendRedirect(adminPresentationUtil.getErrorPath());
//                }
//            }
//        }
//    }
//
//    /**
//     * Get the user menu links from the database and set on the request.
//     * @param request HttpServletRequest
//     * @param adminUser the admin user
//     * @param actionPath the action path
//     */
//    private void setMenuFieldsAndUserName(final HttpServletRequest request, final AdminUser adminUser, final String actionPath){
//
//        // Obtém somente as funcionalidades que devem aparecer no menu
//        final List<Function> accessibleFunctions = adminFacade.getAccessibleFunctions(adminUser.getId());
//        
//        //final Map<FunctionVO, List<FunctionVO>> accessibleFunctionsMap = new LinkedHashMap<FunctionVO, List<FunctionVO>>();
//        final List<FunctionVO> menuItems = new ArrayList<FunctionVO>();
//        
//        final String baseUrl = envUtils.httpsBaseUrl();
//        
//        // setando base url em cada uma
//        for(Function function : accessibleFunctions) {
//            final Function parent = function.getParent();
//            
//            if (parent == null)  {
//                if(ShowMenuFlag.S.equals(function.getShowMenu())){
//                    final FunctionVO parentVO = new FunctionVO(function, baseUrl, false);
//                    if (!menuItems.contains(parentVO) ) {
//                        menuItems.add(parentVO);
//                    }
//                }
//            } else {
//                // check if parent already is on the list
//                FunctionVO parentVO = new FunctionVO(parent, baseUrl, false);
//                int parentIndex = menuItems.indexOf(parentVO);
//                if (parentIndex == -1) {
//                    menuItems.add(parentVO);
//                } else {
//                    // update reference to list item
//                    parentVO = menuItems.get(parentIndex);
//                }
//                
//                // check if this is the active function
//                final boolean isActiveFunction = actionPath != null && actionPath.equals(function.getUrl());
//                if (isActiveFunction) {
//                    parentVO.setActive(true);
//                }
//
//                // add to children list
//                if(ShowMenuFlag.S.equals(function.getShowMenu())){ 
//                    final FunctionVO childrenVO = new FunctionVO(function, baseUrl, false);
//                    parentVO.getChildren().add(childrenVO);
//                }
//            }
//        }
//
//        // sort functions 
//        Collections.sort(menuItems);
//        for (final FunctionVO function : menuItems) {
//            final List<FunctionVO> children = function.getChildren();
//            Collections.sort(children);
//        }
//        
//        request.setAttribute(ACCESSIBLE_FUNCTIONS, menuItems);
//        request.setAttribute(USER_NAME, adminUser.getName());
//    }
//    
//    /**
//     * Init filter method.
//     * @param config FilterConfig
//     * @throws ServletException ServletException
//     */
//    public void init(final FilterConfig config) throws ServletException {
//        
//    }
}
